﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SKG.DXF.Ktx.Danhmuc
{
    using SKG.Plugin;
    using SKG.Extend;
    using DAL.Entities;
    using DevExpress.XtraEditors;
    using System.Data;

    public partial class FrmKtx_Sinhvien : SKG.DXF.FrmInput
    {
        
        #region Override plugin
        public override Form Form { get { return this; } }

        public override Menuz Menuz
        {
            get
            {
                var menu = new Menuz
                {
                    Code = typeof(FrmKtx_Sinhvien).FullName,
                    Parent = typeof(Level2).FullName,
                    Text = "Sinh viên",
                    Level = 0,
                    Order = 12,
                    Picture = @"Icons\Lang.png"
                };
                return menu;
            }
        }
        #endregion

        public FrmKtx_Sinhvien()
        {
            InitializeComponent();

            dockPanel1.SetDockPanel("Nhập liệu");
            dockPanel2.SetDockPanel("Danh sách");

            grvMain.OptionsView.ShowAutoFilterRow = true;
            grvMain.OptionsBehavior.Editable = false;
        }

        private void FrmKtx_Sinhvien_Load(object sender, EventArgs e) 
        {
            cboLophoc.Properties.DataSource = _bll.Ktx_Lophoc.Select();
            cboDoituong.Properties.DataSource = _bll.Ktx_Doituong.Select();            
        }

        private void lokList_EditValueChanged(object sender, EventArgs e)
        {
            //PerformRefresh();
        }

        #region Override
        protected override void SetNullPrompt()
        {
            //txtText.Properties.NullValuePrompt = String.Format("Nhập {0}", lblText.Text.ToBetween(null, ":", Format.Lower));
            //txtNote.Properties.NullValuePrompt = String.Format("Nhập {0}", lblNote.Text.ToBetween(null, ":", Format.Lower));

            base.SetNullPrompt();
        }

        protected override void PerformAdd()
        {
            ClearDataBindings();
            ResetInput();
            txtHoten.Focus();
            ChangeStatus(false);
            base.PerformAdd();
        }

        protected override void PerformDelete()
        {
            var id = (Guid)grvMain.GetFocusedRowCellValue("Id");

            if (id == new Guid()) XtraMessageBox.Show(STR_SELECT, STR_DELETE);
            else
            {
                var cfm = String.Format(STR_CONFIRM, txtHoten.Text);
                var oki = XtraMessageBox.Show(cfm, STR_DELETE, MessageBoxButtons.OKCancel);

                if (oki == DialogResult.OK)
                    if (_bll.Ktx_Sinhvien.Delete(id) != null) PerformRefresh();
                    else XtraMessageBox.Show(STR_UNDELETE, STR_DELETE);
            }

            base.PerformDelete();
        }

        protected override void PerformRefresh()
        {
            LoadData();

            if (_dtb != null)
            {
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            ReadOnlyControl();

            
            base.PerformRefresh();
        }

        protected override void PerformSave()
        {
            switch (_state)
            {
                case State.Add:
                    if (InsertObject())
                    {
                        ResetInput(); LoadData();
                    }
                    break;

                case State.Edit:
                    if (UpdateObject())
                    {
                        ChangeStatus(); ReadOnlyControl();
                        PerformRefresh();
                    }
                    break;

                default:
                    break;
            }

            base.PerformSave();
        }

        protected override void ResetInput()
        {
            txtMSSV.Text = null; 
            txtHoten.Text = null;
            txtDiachi.Text = null;
            radioGioitinh.EditValue = true;
            dteNgaysinh.EditValue = DateTime.Now;
            cboDoituong.ItemIndex = -1;
            cboLophoc.ItemIndex = -1;

            txtMSSV.Focus();

            base.ResetInput();
        }

        protected override void ClearDataBindings()
        {
            txtHoten.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtMSSV.DataBindings.Clear();
            dteNgaysinh.DataBindings.Clear();
            radioGioitinh.DataBindings.Clear();
            cboDoituong.DataBindings.Clear();
            cboLophoc.DataBindings.Clear();

            base.ClearDataBindings();
        }

        protected override void DataBindingControl()
        {
            txtMSSV.DataBindings.Add("EditValue", _dtb, ".MaSV");
            txtHoten.DataBindings.Add("EditValue", _dtb, ".Hoten");
            txtDiachi.DataBindings.Add("EditValue", _dtb, ".Diachi");
            radioGioitinh.DataBindings.Add("EditValue", _dtb, ".Gioitinh");
            dteNgaysinh.DataBindings.Add("EditValue", _dtb, ".Ngaysinh");
            cboDoituong.DataBindings.Add("EditValue", _dtb, ".DoituongId");
            cboLophoc.DataBindings.Add("EditValue", _dtb, ".LophocId");

            base.DataBindingControl();
        }

        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtMSSV.Properties.ReadOnly = isReadOnly;
            cboDoituong.Properties.ReadOnly = isReadOnly;
            txtHoten.Properties.ReadOnly = isReadOnly;
            txtDiachi.Properties.ReadOnly = isReadOnly;
            radioGioitinh.Properties.ReadOnly = isReadOnly;
            dteNgaysinh.Properties.ReadOnly = isReadOnly;
            cboLophoc.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            //if (_state == State.Edit) txtCode.Properties.ReadOnly = true;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override bool UpdateObject()
        {
            try
            {
                if (!ValidInput()) return false;

                var id = (Guid)grvMain.GetFocusedRowCellValue("Id");

                var o = new Ktx_Sinhvien()
                {
                    Id = id,
                   // MaSV = txtMSSV.Text,
                    Hoten = txtHoten.Text,
                    Gioitinh = Convert.ToBoolean(radioGioitinh.EditValue),
                    Ngaysinh = Convert.ToDateTime(dteNgaysinh.EditValue),
                    Diachi = txtDiachi.Text,
                    LophocId = (Guid)cboLophoc.EditValue,
                    DoituongId = (Guid)cboDoituong.EditValue
                };

                //var gui = new Guid();
                //var ok = Guid.TryParse(cboLophoc.EditValue + "", out gui);
                //if (ok) o.ParentId = gui;

                var oki = _bll.Ktx_Sinhvien.Update(o);
                if (oki == null) XtraMessageBox.Show(STR_DUPLICATE, STR_EDIT);

                return oki != null ? true : false;
            }
            catch { return false; }
        }

        protected override bool InsertObject()
        {
            try
            {
                if (!ValidInput()) return false;

                var o = new Ktx_Sinhvien()
                {
                  //  Id = id,
                    MaSV = txtMSSV.Text,
                    Hoten = txtHoten.Text,
                    Gioitinh = Convert.ToBoolean(radioGioitinh.EditValue),
                    Ngaysinh = Convert.ToDateTime(dteNgaysinh.EditValue),
                    Diachi = txtDiachi.Text,
                    LophocId = (Guid)cboLophoc.EditValue,
                    DoituongId = (Guid)cboDoituong.EditValue
                };

                //var gui = new Guid();
                //var ok = Guid.TryParse(cboLophoc.EditValue + "", out gui);
                //if (ok) o.ParentId = gui;

                var oki = _bll.Ktx_Sinhvien.Insert(o);
                if (oki == null) XtraMessageBox.Show(STR_DUPLICATE, STR_ADD);

                return oki != null ? true : false;
            }
            catch { return false; }
        }

        protected override void LoadData()
        {
            _dtb = _bll.Ktx_Sinhvien.Select();
            grcMain.DataSource = _dtb;

            if (_dtb.Rows.Count <= 0) return;
            var tmp = _dtb.Rows[0]["Malop"] + "";
            var o = (Ktx_Lophoc)_bll.Ktx_Lophoc.Select(tmp);

            if (o == null) cboLophoc.Properties.DataSource = null;
            else cboLophoc.Properties.DataSource = _bll.Ktx_Lophoc.Select((object)o);

            if (_dtb.Rows.Count <= 0) return;
            var tmp1 = _dtb.Rows[0]["MaDT"] + "";
            var o1 = (Ktx_Doituong)_bll.Ktx_Doituong.Select(tmp1);

            if (o1 == null) cboDoituong.Properties.DataSource = null;
            else cboDoituong.Properties.DataSource = _bll.Ktx_Doituong.Select((object)o1);
            
            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                MaSV.BestFit(); // fit column STT
            }

            base.LoadData();
        }

        protected override bool ValidInput()
        {
            //var a = txtCode.Text.Length == 0 ? false : true;
            //if (!a) txtCode.Focus();

            var a = txtHoten.Text.Length == 0 ? false : true;
            if (!a) txtHoten.Focus();

            var b = txtDiachi.Text.Length == 0 ? false : true;
            if (!b) txtDiachi.Focus();

            return a && b;
        }
        #endregion

        private const string STR_ADD = "Thêm ngôn ngữ";
        private const string STR_EDIT = "Sửa ngôn ngữ";
        private const string STR_DELETE = "Xoá ngôn ngữ";

        private const string STR_SELECT = "Chọn dữ liệu!";
        private const string STR_CONFIRM = "Có xoá '{0}' không?";
        private const string STR_UNDELETE = "Không xoá được!\nDữ liệu đang được sử dụng.";
        private const string STR_DUPLICATE = "Mã này có rồi";

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }
    }
}